local env = {}
local template = {}

local base = _G
base.env = env
setmetatable(base, template)

function template:__newindex(name, value)
    rawset(env, name, value)
end

function template:__index(name)
    local func = template[name]
    if type(func) == "function" then
        return func(self)
    end

    local value = rawget(env, name)
    if value then
        return value
    end
    return rawget(base, name)
end

function template:__FUNCTION__()
    return debug.getinfo(2).name
end

function template:__LINE__()
    return debug.getinfo(2).currentline
end

function template:__FILE__()
    return getfilename(debug.getinfo(2).source)
end

function template:__DIR__()
    return getpath(debug.getinfo(2).source)
end
